
#import "UIImage+Utilities.h"

@implementation UIImage (Utilities)

-(UIImage *)imageScaledAndCropedToFitSize:(CGSize)size
{
	CGFloat width, height;
	
	if(self.size.width > self.size.height)
    {
		width = round((size.height / self.size.height) * self.size.width);
		height = size.height;
	}
	else if(self.size.width < self.size.height)
    {
		width = size.width;
		height = round((size.width / self.size.width) * self.size.height);
	}
	else
    {
		width = size.width;
		height = size.height;
	}
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, CGImageGetBitsPerComponent(self.CGImage), 4.0 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    
    CGContextDrawImage(context, CGRectMake(-round((width - size.width) / 2.0), -round((height - size.height) / 2.0), width, height), self.CGImage);
    CGImageRef scaledAndCropedImageRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
	
	UIImage *scaledAndCropedImage = [UIImage imageWithCGImage:scaledAndCropedImageRef scale:self.scale orientation:UIImageOrientationUp];
	CGImageRelease(scaledAndCropedImageRef);
	
	return scaledAndCropedImage;
}

-(UIImage *)imageWithRoundedCornersForRadius:(CGFloat)radius
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, self.size.width * self.scale, self.size.height * self.scale, CGImageGetBitsPerComponent(self.CGImage), 4 * self.size.width * self.scale, colorSpace, kCGImageAlphaPremultipliedFirst);
    
    CGRect rect = CGRectMake(0.0, 0.0, self.size.width * self.scale, self.size.height * self.scale);
	
	addRoundedRectToPath(context, rect, radius, radius);

    CGContextClip(context);
    
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, self.size.width * self.scale, self.size.height * self.scale), self.CGImage);
	
	CGContextSetRGBStrokeColor(context, 195.0f / 255.0f, 197.0f / 255.0f, 201.0f / 255.0f, 1.0f);
    strokeRoundedRect(context, rect, radius, radius);
	
    CGImageRef imageWithRoundedCornersRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
	
	UIImage *imageWithRoundedCorners = [UIImage imageWithCGImage:imageWithRoundedCornersRef scale:self.scale orientation:UIImageOrientationUp];
	CGImageRelease(imageWithRoundedCornersRef);
	
    return imageWithRoundedCorners;
}

void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight)
{    
    CGContextSaveGState(context);
    
    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM(context, ovalWidth, ovalHeight);
    
    CGFloat fw = CGRectGetWidth(rect) / ovalWidth;
    CGFloat fh = CGRectGetHeight(rect) / ovalHeight;
    
    CGContextMoveToPoint(context, fw, fh / 2);
    CGContextAddArcToPoint(context, fw, fh, fw / 2, fh, 1);
    CGContextAddArcToPoint(context, 0, fh, 0, fh / 2, 1);
    CGContextAddArcToPoint(context, 0, 0, fw / 2, 0, 1);
    CGContextAddArcToPoint(context, fw, 0, fw, fh / 2, 1);
    
    CGContextClosePath(context);
    
    CGContextRestoreGState(context);
}

void strokeRoundedRect(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
{
    CGContextBeginPath(context);
    
	addRoundedRectToPath(context, rect, ovalWidth, ovalHeight);
    
	CGContextStrokePath(context);
}
@end